// import router from "../../src/router";

var json = { tableName: "userinfo" }

function GetJsonData() {
    var json = {
        "classid": 2,
        "name": $("#tb_name").val(),
        "zlclass": "测试类型1测试类型2测试类型3",
        "pname": "",
        "tel": ""
    };
    return json;
}
$(function () {
    //var formData = new FormData();
    //formData.append("tableName","userinfo");  
    // $.ajax({
    //  type: "POST",
    //   contentType: "application/json; charset=utf-8",
    //  url: domain.testUrl+"/Threeinjection/global/insert", 
    // data:JSON.stringify(GetJsonData()),
    // contentType : false,// 告诉jQuery不要去设置Content-Type请求头
    // processData: false,// 告诉jQuery不要去处理发送的数据
    // dataType: "JSON",
    // success : function(data) {    
    //},    
    // error : function(data) {    
    ///}    
    // });  
    if (localStorage.getItem("username") != null) {
        var e = setInterval(function () {
            $("#loginname").val(localStorage.getItem("username"));
            $("#nloginpwd").val(localStorage.getItem("userPwd"));
            clearInterval(e);
        }, 700);
    }
    if (localStorage.getItem("userPwd") != null) {
        document.getElementById("autologin").checked = true;
    }
    $("#yzm").attr("src", domain.testUrl + "/Threeinjection/user/verificationCode?r=");
    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            $("#loginsubmit").trigger("click");
        }
    });
    //判断是否是ie浏览器
    if (!!window.ActiveXObject || "ActiveXObject" in window) {
        $("#loginname").css("padding-top", "0px");
        $("#nloginpwd").css("padding-top", "0px");
    }
    //alert(window.screen.width);alert(window.screen.height);
    //微信登录和账号登录切换
    $(".login-tab-r").click(function () {

        $(".login-box").css({ "display": "block", "visibility": "visible" });
        $(".qrcode-login").css({ "display": "none" })
    });
    $(".login-tab-l").click(function () {
        swal({
            title: "",
            text: domain.languageval.$t('page.lang_custom'),
            type: "",
            confirmButtonText: domain.languageval.$t('page.lang_ok')
        })
        //swal({text:domain.languageval.$t('page.lang_custom'),confirmButtonText:"HaHa"});
        // console.log(123);
        //swal("OMG!", "功能还在开发中！", "error");
        return;
        $(".login-box").css({ "display": "none" });
        $(".qrcode-login").css({ "display": "block", "visibility": "visible" })
    });
    //点击微信图片显示帮助
    $(".qrcode-img").hover(function () {
        $(".qrcode-img").css({ "left": "0" });
        $(".qrcode-help").css({ "display": "block" });
    }, function () {
        $(".qrcode-img").css({ "left": "64px" });
        $(".qrcode-help").css({ "display": "none" });
    });
    //确认输入用户名密码后，显示验证码
    $("#nloginpwd").blur(function () {
        if (($("#loginname").val() != "") && ($("#nloginpwd").val() != "")) {
            $("#o-authcode").css({ "display": "block" });
        }
        $("#authcode").focus(function () {
            $("#yzm").attr("src", domain.testUrl + "/Threeinjection/user/verificationCode?r=" + newGuid());
        });
    });
    var randomval = random(1, 6);
    window.console.log(`${randomval}`)
    // 登录页面随机背景图片
        $("#banner-bg").css({ 'background': `url("/static/img/login/${randomval}.jpg")`});
    $("#yzm").attr("src", domain.testUrl + "/Threeinjection/user/verificationCode?r=" + newGuid());
});
$(function () {
    $("#yzmh,#yzm").click(function () { // 点击获取新的验证码
        var d = new Date();
        $("#yzm").attr("src", domain.testUrl + "/Threeinjection/user/verificationCode?r=" + newGuid());
    });
});
window.newGuid = function () {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
}
/**
* 产生随机整数，包含下限值，但不包括上限值
* @param {Number} lower 下限
* @param {Number} upper 上限
* @return {Number} 返回在下限到上限之间的一个随机整数
*/
window.random = function (lower, upper) {
    return Math.floor(Math.random() * (upper - lower)) + lower;
};
window.validate = function () { // 登陆表单效验
    $('.msg-wrap').empty();
    //  判断用户名是否为空
    if ($("#loginname").val().length == 0) {
        showMesInfo(domain.languageval.$t('info.lang_loginnamenull'), "error");
        $("#loginname").focus();
        return;
    }
    // 判断密码是否为空
    if ($("#nloginpwd").val().length == 0) {
        showMesInfo(domain.languageval.$t('info.lang_nloginpwdnull'), "error"); $("#nloginpwd").focus();
        return;
    }
    // 判断验证码是否为空
    if ($("#authcode").val().length == 0) {
        showMesInfo(domain.languageval.$t('info.lang_authcodenull'), "error"); $("#authcode").focus();
        return;
    }
    $.fn.jqLoading({ height: 100, width: 240, text: domain.languageval.$t('info.lang_load') });
    setTimeout(function () {
        // $.fn.jqLoading("destroy");location.href="http://192.168.1.150:8089/lgplatform/dist/index.html#/index"
    }, 1000);

    $.ajax({ // ajax請求用戶登陸接口
        type: "POST",
        url: domain.testUrl + "/Threeinjection/user/userLogin",
        data: { "userName": $("#loginname").val(), "userPwd": $("#nloginpwd").val(), "VerificationCode": $("#authcode").val() },
        dataType: "json",
        xhrFields: {
            withCredentials: true
        },
        beforeSend: function (xhr) {
            xhr.withCredentials = true;
        },
        success: function (e) {
            $.fn.jqLoading("destroy");
            window.console.log(e)
            if ("usernameno" == e.result) {
                showMesInfo(domain.languageval.$t('info.lang_loginname'), "error");
                $("#loginname").focus(); return;
            }
            if ("no" == e.result || "mobileno" == e.result) {
                showMesInfo(domain.languageval.$t('info.lang_nloginpwd'), "error");
                $("#nloginpwd").focus(); return;
            }
            if ("codeno" == e.result) {
                showMesInfo(domain.languageval.$t('info.lang_authcode'), "error");
                $("#authcode").focus(); return;
            }
            localStorage.setItem("username", $("#loginname").val());
            document.getElementById("autologin").checked ? localStorage.setItem("userPwd", $("#nloginpwd").val())
                : localStorage.removeItem("userPwd");
            sessionStorage.setItem("username", $("#loginname").val());
            sessionStorage.setItem("sid", e.sessionid);
            sessionStorage.setItem("userId", e.userId);
            sessionStorage.setItem("companyid", e.companyId);
            sessionStorage.setItem("roleId", e.roleId);
            location.href = "/lgplatform/dist/index.html#/";
            // router.push("/register");
            // location.href = "https://www.baidu.com";

            return;
        }, error: function (data, type, err) {
            //alert("123:"+err);
            alert(domain.languageval.$t('info.lang_error') + err)
        }
    });

};
function showMesInfo(msg, type) { //
    $(".msg-warn").hide();
    $('.msg-wrap').empty();
    if (type == 'warn') {
        var info = '<div class="msg-warn"><b></b>' + msg + '</div>';
        $('.msg-wrap').append(info);
    }
    if (type == 'error') {
        var info = '<div class="msg-error"><b></b>' + msg + '</div>';
        $('.msg-wrap').append(info);
    }
}
